package com.inspire.tank;

import com.inspire.tank.strategy.DefaultFireStrategy;

import java.awt.*;
import java.io.Serializable;
import java.util.Random;

/**
 * @author inspire
 * @description
 * @date 2022/6/3 19:36
 */
public class Tank extends GameObject {
    private int priorX,priorY;

    public Dir dir;
    private static final int SPEED=5;

    public static int WIDTH = ResourceMgr.getInstance().goodTankD.getWidth();
    public static int HEIGHT = ResourceMgr.getInstance().goodTankD.getHeight();



    private Random random=new Random();

    private boolean moving=true;

    private boolean living=true;
    public Rectangle rect=new Rectangle();
    public Group group=Group.BAD;


    FireStrategy fireStrategy=new DefaultFireStrategy();

    public Tank(int x, int y, Dir dir,Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group=group;

        rect.x=this.x;
        rect.y=this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

        if(this.group == Group.GOOD){
            String goodFsName = (String) PropertyMgr.get("goodFs");
            try {
                fireStrategy = (FireStrategy) Class.forName(goodFsName).getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            String goodFsName = (String) PropertyMgr.get("badFs");
            try {
                fireStrategy = (FireStrategy) Class.forName(goodFsName).getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        GameModel.getInstance().add(this);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Rectangle getRect() {
        return rect;
    }

    public void stop(){
        moving=false;
    }

    public void paint(Graphics g) {
        if(!living){
            GameModel.getInstance().remove(this);
            return;
        }
        switch(dir) {
            case LEFT:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.getInstance().goodTankL : ResourceMgr.getInstance().badTankL, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.getInstance().goodTankU : ResourceMgr.getInstance().badTankU, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.getInstance().goodTankR : ResourceMgr.getInstance().badTankR, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD? ResourceMgr.getInstance().goodTankD : ResourceMgr.getInstance().badTankD, x, y, null);
                break;
        }

        move();

    }

    @Override
    public int getWidth() {
        return WIDTH;
    }

    @Override
    public int getHeight() {
        return HEIGHT;
    }

    public void back(){
        x=priorX;
        y=priorY;
    }
    private void move() {
        priorX=x;
        priorY=y;
        if(!living){
            return;
        }
        if(!moving){
            return;
        }
        switch (dir){
            case LEFT:
                x-=SPEED;
                break;
            case RIGHT:
                x+=SPEED;
                break;
            case UP:
                y-=SPEED;
                break;
            case DOWN:
                y+=SPEED;
                break;
        }

        if(this.group==Group.BAD && random.nextInt(100)>95){
            this.fire();
        }

        if(this.group==Group.BAD&&random.nextInt(100)>95){
            randomDir();
        }

        boundsCheck();

        //uodate rect
        rect.x=this.x;
        rect.y=this.y;
    }

    private void boundsCheck() {
        if(this.x<2){
            x=2;
        }
        if(this.y<28){
            y=28;
        }
        if(this.x>TankFrame.GAME_WIDTH-Tank.WIDTH-2){
            x=TankFrame.GAME_WIDTH-Tank.WIDTH-2;
        }
        if(this.y>TankFrame.GAME_HEIGHT-Tank.HEIGHT-2){
            y=TankFrame.GAME_HEIGHT-Tank.HEIGHT-2;
        }
    }

    private void randomDir() {

        this.dir=Dir.values()[random.nextInt(4)];//random.nextInt(4);表示4以内的随机数（4个方向）
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public void fire() {

        fireStrategy.fire(this);
    }

    public void die() {
        this.living=false;
    }
}
